//import java.io.*;

class CopyOfEarthSpace1 {
    //char [] spaceword;
    String spaceword;
    String[] alias;
    int[] aliaskey;
    int alias_count;
    int[] earthword;
    String[] coins;
    double[] credits;
    int coin_count;

    public static void main(String args[]) throws Exception {
        //String source;
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        //System.out.println("Welcome, spaceling : What's the amount?");
        //source=br.readLine();
        CopyOfEarthSpace1 e = new CopyOfEarthSpace1();
        //int num=e.convert(source);
        //System.out.println("Earth value =" + num);
        e.add_alias("glob", 'I');
        e.add_alias("prok", 'V');
        e.add_alias("pish", 'X');
        e.add_alias("tegj", 'L');
        e.assign_credits("glob glob", "Silver", 34);
        e.assign_credits("glob prok", "Gold", 57800);
        e.assign_credits("pish pish", "Iron", 3910);
        // AND NOW FOR THE ANSWERS :
        int s1 = e.break_string("pish tegj glob glob");
        //System.out.println(s1);
        double s2 = e.calc_credit("glob prok", "Silver");
        //System.out.println(s2);
        double s3 = e.calc_credit("glob prok", "Gold");
        double s4 = e.calc_credit("glob prok", "Iron");
        System.out.println(s2);
        //System.out.println(s3);
        //System.out.println(s4);
    }

    CopyOfEarthSpace1() {
        alias = new String[20];
        aliaskey = new int[20];
        alias_count = 0;
        spaceword = "IVXLCDM";
        earthword = new int[]{1, 5, 10, 50, 100, 500, 1000};
        coins = new String[20];
        coin_count = 0;
        credits = new double[20];
        //earthword=earthword1;
    }

    double calc_credit(String space_name, String coin_name) throws Exception {
        int coin_count_local = break_string(space_name);
        //Find the corresponding coin name :
        int i, index = -1;
        for (i = 0; i < coins.length; i++) {
            if (coin_name.equals(coins[i])) {
                index = i;
                break;
            }
        }
        //i holds the index now
        if (index != -1)
            return (coin_count_local * credits[index]);
        else
            return 0;
        //return 0;
    }

    void assign_credits(String space_name, String coin_name, int cred_count) throws Exception {
        int coin_count_here = break_string(space_name);
        double single_coin_credit_val = (double) cred_count / coin_count_here;
        coins[coin_count] = coin_name;
        credits[coin_count] = single_coin_credit_val;
        //System.out.println("--->> "+credits[coin_count]+ "   "+coins[coin_count] + " <<-----");
        coin_count++;
    }

    void add_alias(String new_alias, char real_name) throws Exception {
        alias[alias_count] = new_alias;
        aliaskey[alias_count] = match(real_name);
        //System.out.println(alias[alias_count]+" = "+aliaskey[alias_count]);
        alias_count = alias_count + 1;
    }

    int break_string(String input) throws Exception {
        String[] pieces = input.split("\\s");
        String converted = new String();
        for (int i = 0; i < pieces.length; i++) {
            //System.out.println(pieces[i]);
            converted = converted + search_alias(pieces[i]);
        }
        //System.out.println(converted);
        int t = convert(converted);
        //System.out.println(t);
        return t;
    }

    char search_alias(String resolve_alias) {
        for (int i = 0; i < alias.length; i++) {
            if (resolve_alias.equals(alias[i])) {
                return spaceword.charAt(aliaskey[i]);
            }
        }
        return 'z';
    }

    int convert(String source) throws Exception {

        int temp;
        int sum = 0;
        int prev = 10;
        int count;
        char check;
        if (!source.matches("[IVXLCDM]+")) {
            System.out.println("Invalid String....");
            return 0;
        }

        count = 1;
        for (int i = 0; i < source.length(); i++) {
            temp = match(source.charAt(i));
            if (prev == temp) {
                check = spaceword.charAt(temp);
                if (check == 'D' || check == 'V' || check == 'L') {
                    System.out.println("Invalid Num");
                    return 0;
                } else {
                    count++;
                    if (count > 3) {
                        System.out.println("Invalid Num");
                        return 0;
                    }

                }
            } else
                count = 1;
            if (prev < temp) {
                //Add restrictions here before doing the subtraction
                // I can only be subtracted from V and X
                // X can be only subtracted from L and C
                // C can be only subtracted from D and M
                // V, L and D can never be subtracted.
                if (spaceword.charAt(temp) == 'I') {
                    if (!(spaceword.charAt(prev) == 'V' || spaceword.charAt(prev) == 'X')) {
                        System.out.println("Wrong");
                        return 0;
                    }
                }
                if (spaceword.charAt(temp) == 'X') {
                    if (!(spaceword.charAt(prev) == 'L' || spaceword.charAt(prev) == 'C')) {
                        System.out.println("Wrong");
                        return 0;
                    }

                }
                if (spaceword.charAt(temp) == 'C') {
                    if (!(spaceword.charAt(prev) == 'D' || spaceword.charAt(prev) == 'M')) {
                        System.out.println("Wrong");
                        return 0;
                    }
                }
                if (spaceword.charAt(prev) == 'L' || spaceword.charAt(prev) == 'V' || spaceword.charAt(prev) == 'D') {
                    System.out.println("Wrong");
                    return 0;

                }
                sum = sum - earthword[prev];
                sum = sum - earthword[prev];
                sum = sum + earthword[temp];
            } else

                sum = sum + earthword[temp];
            prev = temp;
            //System.out.println(sum);
        }

        //System.out.println("The converted value = "+sum);
        return sum;
    }

    int match(char find) throws Exception {
        return spaceword.indexOf(find);
    }
    /*double coinset(int coin_count,int cred_count,int find_coin_cnt)
     {
         double single_coin_credit_val=(double)cred_count/coin_count;
         double ret_val=single_coin_credit_val*find_coin_cnt;
         return ret_val;

     }*/
}

